<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.bulletinContent" placeholder="关键词搜索" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.bulletinPosition" placeholder="展示位置" clearable style="width: 150px;" class="filter-item">
        <el-option v-for="item in positionList" :key="item.key" :label="item.value" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      :default-sort="{prop: 'typeId', order: null}"
    >
      <el-table-column label="编号" align="center" width="80" fixed>
        <template slot-scope="{row}">
          <span>{{ row.bulletinId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公告概述" align="center" show-overflow-tooltip width="500px">
        <template slot-scope="{row}">
          <span>{{ row.bulletinContent }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容详情" align="center">
        <template slot-scope="{row}">
          <el-popover placement="right" width="600" trigger="click">
            <div style="margin: 10px;">
              <div v-if="row.bulletinText" v-html="row.bulletinText" />
            </div>
            <el-button slot="reference" size="mini">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="展示位置" align="center">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="dark"> {{ positionMap[row.bulletinPosition] }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序权重" align="center">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="plain" type="primary"> {{ row.bulletinRank }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="修改日期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="260px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="primary" size="mini" @click="handleRank(row)">
            排序
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row.bulletinId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showBulletinList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="800px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="100px" style="width: 100%">
        <el-form-item label="公告概述" prop="bulletinContent">
          <el-input v-model="temp.bulletinContent" type="textarea" :rows="2" style="width: 90%" />
        </el-form-item>
        <el-form-item label="展示位置" prop="bulletinPosition">
          <el-select v-model="temp.bulletinPosition" style="width: 200px;" class="filter-item">
            <el-option v-for="item in positionList" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序权重" prop="bulletinRank">
          <el-input-number v-model="temp.bulletinRank" :min="0" :step="1" step-strictly style="text-align: center" size="medium" /><span style="color: gray"> (权重数字越大，展示越靠前)</span>
        </el-form-item>
        <el-form-item label="内容详情" prop="bulletinText" style="width: 90%">
          <tinymce v-if="dialogFormVisible" v-model="temp.bulletinText" :height="150" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="设置排序权重" :visible.sync="dialogRankVisible" width="225px">
      <el-input-number v-model="tempRank.bulletinRank" :min="0" :step="1" step-strictly style="text-align: center" />
      <p style="color: gray">(权重数字越大，展示越靠前)</p>
      <p style="color: gray"></p>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogRankVisible = false">
          取消
        </el-button>
        <el-button size="small" type="primary" @click="setRank()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteBulletin, showBulletinList, saveBulletin, updateRank } from '@/api/bulletin'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import Tinymce from '@/components/Tinymce'

export default {
  name: 'Bulletin',
  components: { Pagination, Tinymce },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        bulletinContent: '',
        bulletinPosition: ''
      },
      temp: {
        bulletinId: undefined,
        bulletinContent: '',
        bulletinText: '',
        bulletinPosition: '',
        bulletinRank: 0
      },
      tempRank: {
        bulletinId: undefined,
        bulletinRank: 0
      },
      dialogFormVisible: false,
      dialogRankVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        bulletinContent: [{ required: true, message: '请填写公告概述', trigger: 'blur' }],
        bulletinText: [{ required: true, message: '请填写公告内容详情', trigger: 'blur' }],
        bulletinRank: [{ required: true, message: '请设置排序权重', trigger: 'change' }],
        bulletinPosition: [{ required: true, message: '请选择公告展示位置', trigger: 'change' }]
      },
      positionList: [
        { key: 0, value: '首页' },
        { key: 1, value: '生活论坛' },
        { key: 2, value: '文体联盟' }
        // { key: 3, value: '业务讨论' }
      ],
      positionMap: {
        0: '首页',
        1: '生活论坛',
        2: '文体联盟'
        // 3: '业务讨论'
      }
    }
  },
  created() {
    this.showBulletinList()
  },
  methods: {
    showBulletinList() {
      this.listLoading = true
      showBulletinList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showBulletinList()
    },
    resetDialog() {
      this.temp = {
        bulletinId: undefined,
        bulletinContent: '',
        bulletinPosition: '',
        bulletinRank: 0
      }
    },
    handleCreate() {
      this.resetDialog()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveBulletin(this.temp).then(() => {
            this.showBulletinList()
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '新增公告成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveBulletin(this.temp).then(() => {
            this.showBulletinList()
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '编辑公告成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(bulletinId) {
      this.$confirm('此操作将删除该公告, 是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBulletin(bulletinId).then(() => {
          this.showBulletinList()
          this.dialogFormVisible = false
          this.$notify({
            title: 'Success',
            message: '删除公告成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    resetRankDialog() {
      this.tempRank = {
        bulletinId: undefined,
        bulletinRank: 0
      }
    },
    handleRank(row) {
      this.resetRankDialog()
      this.tempRank.bulletinId = row.bulletinId
      this.tempRank.bulletinRank = row.bulletinRank
      this.dialogRankVisible = true
    },
    setRank() {
      updateRank(this.tempRank).then(() => {
        this.showBulletinList()
        this.dialogRankVisible = false
        this.$notify({
          title: 'Success',
          message: '设置排序权重成功',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
